/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.easoft.code.pos.ui.beans;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Eliu
 */
public class FormField extends javax.swing.JPanel implements DataEntryField, ReportParamInput {

    private String label;
    private Object value;
    private String toolTip;
    private String sourceName;
    private Object targetObject;
    private Method methodListening;

    @Override
    public Object getTargetObject() {
        return targetObject;
    }

    @Override
    public void setTargetObject(Object targetObject) {
        this.targetObject = targetObject;
    }

    @Override
    public Method getMethodListening() {
        return methodListening;
    }

    @Override
    public void setMethodListening(Method methodListening) {
        this.methodListening = methodListening;
    }

    @Override
    public String getSourceName() {
        return sourceName;
    }

    @Override
    public void setSourceName(String sourceName) {
        this.sourceName = sourceName;
    }

    @Override
    public String getToolTip() {
        return toolTip;
    }

    @Override
    public void setToolTip(String toolTip) {
        this.toolTip = toolTip;
    }

    @Override
    public String getLabel() {
        return label;
    }

    @Override
    public void setLabel(String label) {
        this.fieldLabel.setText(label);
        this.label = label;
    }

    @Override
    public Object getValue() {
        this.value = this.txtInput.getText();
        return value;
    }

    @Override
    public void setValue(Object value) {
        this.value = value;
    }

    public FormField(String label, Object value, String toolTip, String sourceName) {
        this.label = label;
        this.value = value;
        this.toolTip = toolTip;
        this.sourceName = sourceName;
        initComponents();
        this.fieldLabel.setText(label);
        this.txtInput.setToolTipText(toolTip);
    }

    public FormField(String label, Object value, String toolTip) {
        this.label = label;
        this.value = value;
        this.toolTip = toolTip;
        initComponents();
        this.fieldLabel.setText(label);
        this.txtInput.setToolTipText(toolTip);
    }

    /**
     * Creates new form FormField
     */
    public FormField() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        fieldLabel = new javax.swing.JLabel();
        txtInput = new javax.swing.JTextField();

        setLayout(new java.awt.GridLayout(1, 0, 0, 5));

        fieldLabel.setText("Nombre Largo");
        fieldLabel.setPreferredSize(new java.awt.Dimension(150, 16));
        add(fieldLabel);

        txtInput.setText("jTextField2");
        txtInput.setPreferredSize(new java.awt.Dimension(180, 22));
        txtInput.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtInputFocusLost(evt);
            }
        });
        add(txtInput);
    }// </editor-fold>//GEN-END:initComponents

    private void txtInputFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtInputFocusLost
        if (methodListening != null) {
            try {
                Class<?>[] paramsType = methodListening.getParameterTypes();
                Class paramType = paramsType[0];
                try {
                    methodListening.invoke(targetObject, paramType.cast(txtInput.getText()));
                    System.out.println("tradicional setting..." + txtInput.getText());
                } catch (ClassCastException caex) {
                    try {
                        System.out.println("fallo setting tradicional..lanzando alterno");
                        Method alt = paramType.getMethod("valueOf", String.class);
                        Object val = alt.invoke(null, txtInput.getText());
                        methodListening.invoke(targetObject, val);
                    } catch (NoSuchMethodException | SecurityException | IllegalAccessException ex) {
                        System.out.println("no fue posible el setting alterno");
                    }

                }
            } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | SecurityException ex) {
                Logger.getLogger(FormField.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_txtInputFocusLost
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel fieldLabel;
    private javax.swing.JTextField txtInput;
    // End of variables declaration//GEN-END:variables

    @Override
    public void ready() {
        this.fieldLabel.setText(label);
        this.txtInput.setText(value != null ? value.toString() : "");
    }

    @Override
    public String getInputKeyName() {
        return this.sourceName;
    }

    @Override
    public Object getCurrentValue() {
        return this.txtInput.getText();
    }

    @Override
    public void cleanValue() {
        this.txtInput.setText("");
    }
}
